package zhupf.test.lamda;

import java.util.concurrent.RecursiveTask;

public class ForkJoinCulat extends RecursiveTask<Long> {
    private long start;
    private long end;
    private static final long THRESHOLD = 10000L;

    public ForkJoinCulat(long start,long end){
        this.start = start;
        this.end = end;
    }
    protected Long compute() {
        long length = end - start;
        if(length <= THRESHOLD){
            long sum = 0L;
            for(long i = start;i<= end ;i++){
                sum += i;
            }
            return sum;
        }else{
            long middle = (start+end)/2;
            ForkJoinCulat left = new ForkJoinCulat(start,middle);
            left.fork();
            ForkJoinCulat right = new ForkJoinCulat(middle+1,end);
            right.fork();
            return left.join()+right.join();
        }
    }
}
